package cn.dy.sbo.web.ope;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import cn.common.lib.springside.orm.ExtPropertyFilter;
import cn.dy.sbo.core.FlowHisManager;
import cn.dy.sbo.core.OrdersManager;
import cn.dy.sbo.model.FlowHis;
import cn.dy.sbo.model.Orders;
import cn.dy.sbo.model.StepField;
import cn.dy.sbo.util.JsonUtil;
import cn.dy.sbo.vo.ColVO;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/ope")
public class OrderAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    
    private Long id;

    protected Page<Orders>         page             = new Page<Orders>(10);
    
    private Orders orders;
    
    private List<FlowHis> flowHis;
    
    @Autowired
    private OrdersManager ordersManager;
    @Autowired
    private FlowHisManager flowHisManager;
    
    @Override
    public String execute() throws Exception
    {
    	List<ExtPropertyFilter> filters = ExtPropertyFilter
        .extBuildFromHttpRequest(Struts2Utils.getRequest());
    	
    	page = ordersManager.extSearch(page, filters);
        return SUCCESS;
    }
    
    public String detail() 
    {
    	orders = ordersManager.get(id);
    	flowHis = flowHisManager.findByOrders(id);
    	try{
	    	Class<FlowHis> c = FlowHis.class;
	    	for(FlowHis f: flowHis){
	    		List<StepField> fields = f.getStep().getFields();
	    		List<ColVO> list = new ArrayList<ColVO>();
	    		for(StepField sf:fields){
	    			ColVO vo = new ColVO();
	    			vo.setName(sf.getFieldName());
	    			PropertyDescriptor pd = new PropertyDescriptor(sf.getField(),c);
	    			Method m = pd.getReadMethod();
	    			vo.setValue((String) m.invoke(f));
	    			list.add(vo);
	    		}
	    		f.setColJson(JsonUtil.objectToJson(list));
	    	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return "detail";
    }
    
    
	public Page<Orders> getPage() {
		return page;
	}
	public void setPage(Page<Orders> page) {
		this.page = page;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public Orders getOrders() {
		return orders;
	}

	public List<FlowHis> getFlowHis() {
		return flowHis;
	}
	
	
}
